package com.kingyea.mobilepolice.menu.controller;

import com.kingyea.mobilepolice.menu.bean.Menu;
import com.kingyea.mobilepolice.menu.bean.MenuCondition;
import com.kingyea.mobilepolice.menu.service.MenuService;
import com.kingyea.mobilepolice.security.SecurityUtils;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hx
 * @version v1.0.0
 * @description Menu 控制层
 * @date Created in 2018-04-17 15:13:39
 */
@RequestMapping(value = "/menu")
@RestController
public class MenuController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<Menu> list() throws Exception {
        return menuService.search(null);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Menu detail(@PathVariable Integer id) throws Exception {
        return menuService.getById(id);
    }

    @PostMapping(value = "/getList")
    public Object getList(@RequestBody MenuCondition condition) throws Exception {
        return new Result<>(menuService.search(condition));
    }

    /**
     * 根据用户id查询已有权限的菜单
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PreAuthorize("hasAuthority('add-menu-user')")
    @PostMapping(value = "/searchMenuTreeByuserId/{userId}")
    public Object seatchMenuTreeByuserId(@PathVariable String userId) throws Exception {
        return new Result<>(menuService.searchMenuTreeByuserId(userId));
    }

    /**
     * 查询已有权限已添加的菜单
     *
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/searchAlreadyDdded")
    public Object searchAlreadyDdded() throws Exception {
        User user = SecurityUtils.getCurrentUser();
        return new Result<>(menuService.searchByUserId(user.getUserId(), 1));
    }

    /**
     * 查询已有权限未添加的菜单
     *
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/searchNotAdded")
    public Object searchNotAdded() throws Exception {
        User user = SecurityUtils.getCurrentUser();
        return new Result<>(menuService.searchByUserId(user.getUserId(), 0));
    }

    /**
     * 创建
     *
     * @param menu
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Menu create(@RequestBody Menu menu) throws Exception {
        return menuService.save(menu);
    }

    /**
     * 更新
     *
     * @param menu
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public Menu update(@RequestBody Menu menu) throws Exception {
        return menuService.update(menu);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public int delete(@PathVariable Integer id) throws Exception {
        return menuService.deleteById(id);
    }

}
